package wechat

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
)

type TemplateSchoolBusNoticeDataValue struct {
	Value string `json:"value"`
}

type TemplateSchoolBusNoticeData struct {
	First    TemplateSchoolBusNoticeDataValue `json:"first"`
	Keyword1 TemplateSchoolBusNoticeDataValue `json:"keyword1"` // 学生姓名
	Keyword2 TemplateSchoolBusNoticeDataValue `json:"keyword2"` // 站点名称
	Keyword3 TemplateSchoolBusNoticeDataValue `json:"keyword3"` // 乘车状态
	Keyword4 TemplateSchoolBusNoticeDataValue `json:"keyword4"` // 刷卡时间
	Remark   TemplateSchoolBusNoticeDataValue `json:"remark"`
}

type TemplateSchoolBusNotice struct {
	Touser     string                      `json:"touser"`
	TemplateId string                      `json:"template_id"`
	Url        string                      `json:"url"`
	Data       TemplateSchoolBusNoticeData `json:"data"`
}

type TemplateSchoolBusNoticeRes struct {
	Errcode int    `json:"errcode"`
	Errmsg  string `json:"errmsg"`
	Msgid   int64  `json:"msgid"`
}

// 发送校车通知
func SendSchoolBusNotice(openid, first, remark, url, k1, k2, k3, k4 string) {
	t := TemplateSchoolBusNotice{
		Touser:     openid,
		TemplateId: beego.AppConfig.String("schoolbusnoticeid"),
		Url:        url,
		Data: TemplateSchoolBusNoticeData{
			First:    TemplateSchoolBusNoticeDataValue{Value: first},
			Keyword1: TemplateSchoolBusNoticeDataValue{Value: k1},
			Keyword2: TemplateSchoolBusNoticeDataValue{Value: k2},
			Keyword3: TemplateSchoolBusNoticeDataValue{Value: k3},
			Keyword4: TemplateSchoolBusNoticeDataValue{Value: k4},
			Remark:   TemplateSchoolBusNoticeDataValue{Value: remark},
		},
	}
	content, err := json.Marshal(t)
	if err != nil {
		logs.Error("can not marshal school bus notice struct:", err)
		return
	}
	ret := SendTemplateMsg("anban", string(content))
	res := TemplateSchoolBusNoticeRes{}
	err = json.Unmarshal([]byte(ret), &res)
	if err != nil {
		logs.Error("can not unmarshal send school bus notice response:", err)
		return
	}
	if res.Errcode != 0 {
		logs.Error("send school bus notice fail:", res.Errmsg)
		return
	}
}
